﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using Emoney.DataAccess;
using LiuYan.Base;
using LiuYan.Entity;


namespace LiuYan.Service
{
    public class WxMenuService
    {
        /// <summary>
        /// query this parent menu
        /// </summary>
        ///select parent menu count <returns></returns>
        public DataTable GetPMenu()
        {
            string sql = "select * from [dbo].[TB_ParentMenu] order  by [Order] asc";
            return SqlHelper.ExecuteDataset(Connection.conStr, CommandType.Text, sql).Tables[0];
        }

        /// <summary>
        /// query this parent menu his child menu
        /// </summary>
        ///select parent menu count <returns></returns>
        public DataTable GetCMenu(int pId)
        {
            string sql = "select * from [dbo].[TB_ChildMenu] where Pid=" + pId + " order  by [Order] asc";
            return SqlHelper.ExecuteDataset(Connection.conStr, CommandType.Text, sql).Tables[0];
        }
        /// <summary>
        /// get parent menu quantitty
        /// </summary>
        /// <returns></returns>
        public int GetPMenuCount()
        {
            string sql = "select count(*) as count from [dbo].[TB_ParentMenu]";
            var obj = SqlHelper.ExecuteScalar(Connection.conStr, CommandType.Text, sql);
            return Convert.ToInt32(obj);
        }
        /// <summary>
        ///get child menu quantity
        /// </summary>
        /// <returns></returns>
        public int GetCMenuCount(int pid)
        {
            string sql = "select count(*) as count from [dbo].[TB_ChildMenu] where Pid="+pid;
            var obj = SqlHelper.ExecuteScalar(Connection.conStr, CommandType.Text, sql);
            return Convert.ToInt32(obj);
        }

        /// <summary>
        /// edit parent menu
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int EditP(TB_ParentMenu entity)
        {
          SqlParameter[] parameters=new SqlParameter[]
          {
              new SqlParameter("@Id",SqlDbType.Int),
              new SqlParameter("@MenuName",SqlDbType.VarChar,550), 
              new SqlParameter("@Type",SqlDbType.VarChar,550), 
              new SqlParameter("@Key",SqlDbType.VarChar,550), 
              new SqlParameter("@Order",SqlDbType.Int), 
          };
            parameters[0].Value = entity.Id;
            parameters[1].Value = entity.MenuName;
            parameters[2].Value = entity.Type;
            parameters[3].Value = entity.Key;
            parameters[4].Value = entity.Order;
            string sql = entity.Id == 0 ? "insert into [dbo].[TB_ParentMenu](MenuName,Type,[Key],[Order])values(@MenuName,@Type,@Key,@Order)" :
                "update [dbo].[TB_ParentMenu] set MenuName=@MenuName,Type=@Type,[Key]=@Key,[Order]=@Order where id=@Id";
            return SqlHelper.ExecuteNonQuery(Connection.conStr, CommandType.Text, sql, parameters);
        }
        /// <summary>
        /// edit child menu
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int EditC(TB_ChildMenu entity)
        {
            SqlParameter[] parameters = new SqlParameter[]
          {
              new SqlParameter("@Id",SqlDbType.Int),
              new SqlParameter("@MenuName",SqlDbType.VarChar,550), 
              new SqlParameter("@Type",SqlDbType.VarChar,550), 
              new SqlParameter("@Key",SqlDbType.VarChar,550), 
              new SqlParameter("@Pid",SqlDbType.Int), 
               new SqlParameter("@Order",SqlDbType.Int),
          };
            parameters[0].Value = entity.Id;
            parameters[1].Value = entity.MenuName;
            parameters[2].Value = entity.Type;
            parameters[3].Value = entity.Key;
            parameters[4].Value = entity.Pid;
            parameters[5].Value = entity.Order;
            string sql = entity.Id == 0 ? "insert into [dbo].[TB_ChildMenu](MenuName,Type,[Key],Pid,[Order])values(@MenuName,@Type,@Key,@Pid,@Order)" :
                "update [dbo].[TB_ChildMenu] set MenuName=@MenuName,Type=@Type,[Key]=@Key,Pid=@Pid,[Order]=@Order where id=@Id";
            return SqlHelper.ExecuteNonQuery(Connection.conStr, CommandType.Text, sql, parameters);
        }

        /// <summary>
        ///get menu
        /// </summary>
        /// <returns></returns>
        public DataSet GetList()
        {
            string sql = "select  * from [dbo].[TB_ParentMenu] order  by [Order] asc" +
                         " select * from [dbo].TB_ChildMenu order  by [Order] asc";
            var set= SqlHelper.ExecuteDataset(Connection.conStr, CommandType.Text, sql);
            sql = "select Title from [dbo].[TB_ImgArticle] where id={0}";
            set.Tables[0].TableName = "pmenu";
            set.Tables[1].TableName = "cmenu";
            set.Tables[0].Columns.Add("Title", typeof (string));
            set.Tables[1].Columns.Add("Title", typeof(string));
            foreach (DataRow  row in  set.Tables[1].Rows)
            {
                if (row["Type"].ToString() == "click")
                {
                    sql = string.Format(sql, row["Key"]);
                    row["Title"]=SqlHelper.ExecuteScalar(Connection.conStr, CommandType.Text, sql).ToString();
                }
            }
            foreach (DataRow row in set.Tables[0].Rows)
            {
                if (row["Type"].ToString() == "click")
                {
                    sql = string.Format(sql, row["Key"]);
                    row["Title"] = SqlHelper.ExecuteScalar(Connection.conStr, CommandType.Text, sql).ToString();
                }
            }
            return set;
        }

       /// <summary>
       /// delete menu
       /// </summary>
       /// <param name="id"></param>
       /// <param name="menu"></param>
       /// <returns></returns>
        public int DelMenu(string id,string menu)
        {
            string sql = "";
            if (menu == "f")//删除父级菜单
            {
                sql = "select count(*) from [dbo].[TB_ChildMenu] where pid=" + id;
                if (Convert.ToInt32(SqlHelper.ExecuteScalar(Connection.conStr, CommandType.Text, sql)) > 0)
                {
                    return -1;
                }
                sql = "delete [dbo].[TB_ParentMenu] where id=" + id;
               return SqlHelper.ExecuteNonQuery(Connection.conStr, CommandType.Text, sql);
            }
            else
            {
                sql = "delete [dbo].[TB_ChildMenu] where id=" + id;
                return SqlHelper.ExecuteNonQuery(Connection.conStr, CommandType.Text, sql);
            }
            return 0;
        }

        /// <summary>
        /// validation parent menu is exsit 
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int ExsitFMenu(int id)
        {
            string sql = "select count(*) from [dbo].[TB_ParentMenu] where id="+id;
            return Convert.ToInt32(SqlHelper.ExecuteScalar(Connection.conStr, CommandType.Text, sql));
        }
        /// <summary>
        ///  validation child menu is exsit 
        /// </summary>
        /// <param name="id"></param>
        /// <param name="pId"></param>
        /// <returns></returns>
        public int ExsitCMenu(int id,int pId)
        {
            string sql = "select count(*) from [dbo].[TB_ChildMenu] where id="+id+" and Pid="+pId;
            return Convert.ToInt32(SqlHelper.ExecuteScalar(Connection.conStr, CommandType.Text, sql));
        }
    }
}
